package com.dlxs.studyproject.ui.home;

import android.content.Context;
import android.graphics.Point;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;


import java.util.List;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.dlxs.studyproject.R;

import devin.com.picturepicker.utils.ImageLoader;

/**
 * <p>Description:
 * <p>Company：
 * <p>Email:bjxm2013@163.com
 * <p>Created by Devin Sun on 2017/10/3.
 */
public class ReleaseImgAdapter extends RecyclerView.Adapter<ItemReleaseImgHolder> {



    private OnItemClickListener listener;

    private Context context;
    private List<String> uploadImgList;

    private final int w;

    public ReleaseImgAdapter(Context context, List<String> uploadImgList) {
        this.context = context;
        this.uploadImgList = uploadImgList;
        w =getScreenWidth(context) / 3;
    }



    /**
     * 得到屏幕的宽 适用安卓3.0以上
     *
     * @return 屏幕的宽  px单位
     */
    public static int getScreenWidth(Context context) {

        // 得到手机窗体管理者
        WindowManager wm = (WindowManager)context
                .getSystemService(Context.WINDOW_SERVICE);

        // 3.0才有的API
        Point outSize = new Point();
        wm.getDefaultDisplay().getSize(outSize);
        return outSize.x;

    }

    /**
     * item的监听
     */
    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }


    /**
     * 设置item的监听器
     *
     * @param listener ""
     */
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }


    @Override
    public int getItemCount() {
        if (uploadImgList == null) {
            return 1;
        } else if (uploadImgList.size() <3) {
            return uploadImgList.size() + 1;
        } else {
            return uploadImgList.size();
        }
    }


    @Override
    public ItemReleaseImgHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.item_release_img, null);
        return new ItemReleaseImgHolder(view);
    }

    @Override
    public void onBindViewHolder(final ItemReleaseImgHolder holder, int position) {

        ViewGroup.LayoutParams layoutParams = holder.getIvReleaseImg().getLayoutParams();
        layoutParams.width = w;
        layoutParams.height = w;
        holder.getIvReleaseImg().setLayoutParams(layoutParams);

        //有图片
        if (position < uploadImgList.size()) {
            //显示删除按钮
            holder.getIvDeleteImage().setVisibility(View.VISIBLE);

            RequestOptions requestOptions =
                    new RequestOptions()
                            .placeholder(R.drawable.icon_back)
                            .error(R.drawable.icon_back);

            Glide.with(context)
                    .load(uploadImgList.get(position))
                    .apply(requestOptions)
                    .into(holder.getIvReleaseImg());

        } else {//没有图片的位置
            //隐藏删除按钮
            holder.getIvDeleteImage().setVisibility(View.GONE);
            holder.getIvReleaseImg().setImageResource(R.drawable.icon_add_img);
        }


        holder.getIvReleaseImg().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onItemClick(holder.getIvReleaseImg(), holder.getAdapterPosition());
                }
            }
        });

        holder.getIvDeleteImage().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                uploadImgList.remove(holder.getAdapterPosition());
                notifyDataSetChanged();
            }
        });
    }
}